<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/taglib.jsp" %>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>转账</title>

    <!-- Bootstrap -->
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/Transfer.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <script type="text/javascript" src="/js/web3.min.js"></script>
    <script type="text/javascript" src="/js/bignumber.js"></script>

    <script src="/js/jquery-2.2.3.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/Transfer.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(function () {
            alert("在进行转账操作之前，请在geth客户端下使用以下命令解锁账户。\n\n personal.unlockAccount('yourAddress')");

            // 初始化web3对象
            if (typeof web3 !== 'undefined') {
                web3 = new Web3(web3.currentProvider);
            } else {
                web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
            }

            var EDUContract = web3.eth.contract([ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string", "value": "edu" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256", "value": "1000" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8", "value": "2" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "burn", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "burnFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string", "value": "edu" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" }, { "name": "_extraData", "type": "bytes" } ], "name": "approveAndCall", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" }, { "name": "", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "addr", "type": "address" } ], "name": "getBalance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256", "index": 0, "typeShort": "uint", "bits": "256", "displayName": "initial Supply", "template": "elements_input_uint", "value": "1000" }, { "name": "tokenName", "type": "string", "index": 1, "typeShort": "string", "bits": "", "displayName": "token Name", "template": "elements_input_string", "value": "edu" }, { "name": "decimalUnits", "type": "uint8", "index": 2, "typeShort": "uint", "bits": "8", "displayName": "decimal Units", "template": "elements_input_uint", "value": "2" }, { "name": "tokenSymbol", "type": "string", "index": 3, "typeShort": "string", "bits": "", "displayName": "token Symbol", "template": "elements_input_string", "value": "edu" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ]);
            var EDU = EDUContract.at("0x4ca024FdA1fF2d4419d51f07D588add5D37b67ab");

            var account = '${address}';
            console.log(account);
            var etherBalance = web3.fromWei(web3.eth.getBalance(account).toNumber(), 'ether');
            var eduBalance = EDU.getBalance.call(account);

            document.getElementById("etherBalance").innerText = "Ether 余额： " + etherBalance;
            document.getElementById("eduBalance").innerText = "EDU 余额： " + eduBalance;
        });

        /*
         发送交易
         */
        function sendTransaction() {


            var EDUContract = web3.eth.contract([ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string", "value": "edu" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256", "value": "1000" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8", "value": "2" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "burn", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "burnFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string", "value": "edu" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" }, { "name": "_extraData", "type": "bytes" } ], "name": "approveAndCall", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" }, { "name": "", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256", "value": "0" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "addr", "type": "address" } ], "name": "getBalance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256", "index": 0, "typeShort": "uint", "bits": "256", "displayName": "initial Supply", "template": "elements_input_uint", "value": "1000" }, { "name": "tokenName", "type": "string", "index": 1, "typeShort": "string", "bits": "", "displayName": "token Name", "template": "elements_input_string", "value": "edu" }, { "name": "decimalUnits", "type": "uint8", "index": 2, "typeShort": "uint", "bits": "8", "displayName": "decimal Units", "template": "elements_input_uint", "value": "2" }, { "name": "tokenSymbol", "type": "string", "index": 3, "typeShort": "string", "bits": "", "displayName": "token Symbol", "template": "elements_input_string", "value": "edu" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ]);
            var EDU = EDUContract.at("0x4ca024FdA1fF2d4419d51f07D588add5D37b67ab");

            // 获取交易基本信息
            var coinType = $("#coinType").val();
            var fromAddress = '${address}';
            var etherBalance = web3.eth.getBalance(fromAddress).toNumber();
            var eduBalance = EDU.getBalance.call(fromAddress);
            var toAddress = $("#account_to").val();
            var money;

            console.log(fromAddress + " " + toAddress + " " + etherBalance + " " + eduBalance + " " + money + " " + coinType);

            if (!web3.isAddress(toAddress)) {
                alert("请指定合法接收人！");
            } else if ($("#amount-number").val() == "") {
                alert("请指定发送数量！");
            } else {
                if (coinType == "Ether") {
                    money = web3.toWei($("#amount-number").val(), 'ether');
                    if (money >= etherBalance) {
                        alert("该账户资金不足！");
                    } else {
                        var transaction = {
                            from: fromAddress,
                            to: toAddress,
                            value: money
                        };

                        web3.eth.sendTransaction(transaction, function (err) {
                            if (!err) {
                                alert("交易成功！");
                            } else {
                                alert("交易失败！\n\n未解锁账户或者交易金额不够！");
                            }
                        });
                    }
                } else {
                    money = $("#amount-number").val();
                    if (money >= eduBalance) {
                        alert("该账户资金不足！");
                    } else {
                        var txHash = EDU.transfer.sendTransaction(toAddress, money, {from: fromAddress}, function (err) {
                            if (!err) {
                                alert("交易成功！请在geth下使用 miner.start() 操作同步区块。\n挖矿结束后，使用 miner.stop() 结束挖矿！");
                            } else {
                                alert("交易失败！请解锁账户或者减少交易金额！");
                            }
                        });
                    }
                }
            }
        }
    </script>

    <c:import url="/WEB-INF/jsp/frontend/head.jsp" />
    <c:import url="/WEB-INF/jsp/frontend/activity/script.jsp" />
</head>
<body>

<div class="head">
    <c:import url="/WEB-INF/jsp/frontend/header.jsp"/>
</div>

<div class="container-wrap transfer-body">

    <div class="container">
        <div class="row transfer-container">
            <div class="col-md-11 transfer-head">转账</div>
            <div class="col-md-11 transfer-divider"></div>
            <div class="col-md-8 transfer-from">
                <div class="transfer-label transfer-h1">我的账户</div>
                <div class="transfer-account">
                    <div class="account-number">${address}</div>
                    <div class="transfer-remainder" id="etherBalance"></div>
                    <div class="transfer-remainder" id="eduBalance"></div>
                </div>
            </div>
            <div class="col-md-3 transfer-info">
                <span class="from-info">
                    <img class="img-circle from-portrait" src="/images/wallet/wallet-portrait.png">
                    <span class="wallet-h1 from-username">${username}</span>
                </span>
            </div>
            <div class="col-md-11 transfer-mid">
                <img class="transfer-icon" src="/images/wallet/transfer.png">
                <span class="transfer-h1">转账到</span>
            </div>
            <div class="col-md-8 transfer-to">
                <label class="transfer-label transfer-h1" for="account_to">对方账户</label>
                <input class="transfer-account account-number" placeholder="0x0000000000000000000..."
                       required="required" type="text" id="account_to"/>
            </div>
            <div class="col-md-11 transfer-amount">
                <form role="form" action method="post" class="login-form" id="login-form">
                    <label for="amount-number" class="amount-label transfer-h1">转账金额</label>
                    <input type="text" name="amount-number" class="amount-input" id="amount-number" required="required">
                    <span class="amount-conversion">
                            <select class="amount-input" id="coinType">
                                <option value="Ether">Ether</option>
                                <option value="EDU">EDU</option>
                            </select>
                        </span>
                    <input type="button" value="确认转账" class="transfer-define" onclick="sendTransaction()">
                </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>
